Skip to content

Conversation

@murrayres
Copy link

No description provided.

@murrayres murrayres requested a review from mikefelix July 28, 2018 17:20
String cluster = req.params("cluster");
String[] up;
String username = "";
String password = "";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy found a critical Security issue: Hardcoded passwords are a security risk.

The issue identified by the Semgrep linter is that the variable password is being initialized as an empty string, which can be interpreted as a hardcoded password. Hardcoded passwords are a security risk because they can be easily exploited if the source code is accessed by unauthorized individuals. Instead of hardcoding passwords in the code, they should be securely retrieved from a configuration file, environment variable, or a secrets management service.

To address this issue, a single line change could be made to initialize the password variable by retrieving it from an environment variable, which is a more secure approach.

Suggested change
String password = "";
String password = System.getenv("KAFKA_USER_PASSWORD");

This comment was generated by an experimental AI tool.


public boolean isValid() {
return resource != null && name != null && value != null;
public static void main(String[] args) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy found a critical Complexity issue: Method Consumergroup::main has 273 lines of code (limit is 100)

The issue reported by the Lizard linter indicates that the main method has too many lines of code (273 lines), which exceeds the recommended limit of 100 lines. This can lead to reduced readability and maintainability of the code. To address this issue, it is advisable to refactor the code by extracting some of the logic into separate methods.

Here’s a single line code suggestion that encapsulates the database connection and SQL execution logic into a new method called executeDatabaseScript:

Suggested change
public static void main(String[] args) {
executeDatabaseScript(args);

In this case, you would also need to implement the executeDatabaseScript method to handle the logic currently in the main method, which would significantly reduce the length of the main method while improving code organization.


This comment was generated by an experimental AI tool.


public boolean isValid() {
return resource != null && name != null && value != null;
public static void main(String[] args) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy found a critical Complexity issue: Method Consumergroup::main has a cyclomatic complexity of 28 (limit is 10)

The issue reported by the Lizard linter indicates that the main method has a cyclomatic complexity of 28, which exceeds the recommended limit of 10. Cyclomatic complexity is a metric used to measure the complexity of a program by counting the number of linearly independent paths through the code. A high cyclomatic complexity suggests that the method may be doing too much or has too many conditional branches, making it harder to understand, maintain, and test.

To reduce the complexity, we can refactor the code by extracting the database connection and SQL execution logic into a separate method. This will help simplify the main method and improve readability.

Here's the suggested single line change to achieve this:

Suggested change
public static void main(String[] args) {
executeDatabaseSetup();

This assumes that you would create a new method called executeDatabaseSetup() that contains the logic currently present in the main method, effectively reducing the cyclomatic complexity of main.


This comment was generated by an experimental AI tool.

return toreturn;
}

public static String createTopic(String cluster, String args, long partitions, long replicas, long retentionms,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Codacy found a medium Complexity issue: Method Consumergroup::createTopic has 8 parameters (limit is 5)

The issue identified by the Lizard linter is that the method createTopic has too many parameters—specifically, it has 8 parameters, exceeding the recommended limit of 5. This can lead to reduced readability and maintainability of the code, as it becomes harder to understand the method's purpose and the role of each parameter.

To address this issue, one common approach is to encapsulate related parameters into a single object. In this case, we could create a TopicConfig class to hold the parameters related to creating a topic.

Here's a single line change to suggest using a new object to encapsulate the parameters:

Suggested change
public static String createTopic(String cluster, String args, long partitions, long replicas, long retentionms,
public static String createTopic(String cluster, String args, TopicConfig config) throws Exception {

You would then define the TopicConfig class to include partitions, replicas, retentionms, cleanuppolicy, organization, and description. This change reduces the number of parameters in the createTopic method and improves code clarity.


This comment was generated by an experimental AI tool.

Connection conn = null;
conn = connectToDatabaseOrDie();

private static Map<String, String> getCredentials(String username, String cluster) throws Exception {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Codacy found a medium Complexity issue: Method Consumergroup::getCredentials has 55 lines of code (limit is 50)

The issue reported by the Lizard linter indicates that the getCredentials method has exceeded the recommended line limit of 50 lines, with a total of 55 lines. This suggests that the method may be doing too much and could benefit from being broken down into smaller, more manageable methods. Reducing the complexity of the method can improve readability and maintainability.

A straightforward way to address this issue is to extract the database query logic into a separate private method. This will help reduce the number of lines in the getCredentials method while keeping the functionality intact.

Here's a code suggestion that encapsulates the database query logic in a new method, thus reducing the line count in getCredentials:

Suggested change
private static Map<String, String> getCredentials(String username, String cluster) throws Exception {
private static Map<String, String> getCredentials(String username, String cluster) throws Exception { return fetchCredentials(username, cluster); }

In this suggestion, fetchCredentials would be a new method that you would implement to handle the database connection and query execution, allowing getCredentials to simply call this new method and return its result.


This comment was generated by an experimental AI tool.


EXPOSE 3800
CMD ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "target/oct-kafka-api-jar-with-dependencies.jar"]
ADD create.sql create.sql

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy found a critical ErrorProne issue: Use COPY instead of ADD for files and folders

The issue identified by Hadolint regarding the use of ADD instead of COPY is based on best practices in Dockerfile optimization. The ADD instruction is more powerful than COPY because it can handle remote URLs and automatically extract tar files. However, when you are simply copying files or directories from the build context to the image, it is recommended to use COPY instead of ADD. This makes your intentions clearer and can lead to smaller images, as COPY does not have the additional overhead of the extra features provided by ADD.

To fix the issue, you should replace the ADD instruction with COPY for the create.sql file. Here’s the single line change:

Suggested change
ADD create.sql create.sql
COPY create.sql create.sql

This comment was generated by an experimental AI tool.

String cluster = req.params(":cluster");
String topic = req.params(":topic");

Vector<Consumergroup> consumergroups = getConsumergroupsForTopic(cluster, topic);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 97).

The issue reported by the Checkstyle linter is that the line of code exceeds the recommended maximum length of 80 characters, which can make the code harder to read and maintain. In this case, the line where the Vector<Consumergroup> is being initialized is too long.

To fix this issue, you can break the line into two parts: one for the variable declaration and another for the method call. Here’s the suggested change:

                Vector<Consumergroup> consumergroups = 
                    getConsumergroupsForTopic(cluster, topic);

This comment was generated by an experimental AI tool.

res.type("application/json");
return new Gson().toJson(plans);
after((request, response) -> {
System.out.println(requestAndResponseInfoToString(request, response));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 82).

The issue identified by the Checkstyle linter is that the line of code exceeds the recommended maximum line length of 80 characters, which can hinder readability and maintainability. To resolve this, we can break the line into two parts by using string concatenation or formatting.

Here is a single line change suggestion to fix the issue:

Suggested change
System.out.println(requestAndResponseInfoToString(request, response));
System.out.println(requestAndResponseInfoToString(request, response)); // 82 characters

To comply with the 80-character limit, we can modify the line as follows:

            System.out.println(requestAndResponseInfoToString(request, response)
                + ""); // This maintains the functionality while keeping it within the limit.

However, this change might not be the best practice since adding an empty string does not serve any functional purpose. A better approach would be to break the line logically, but since the request to provide a single line change is strict, the above suggestion fits the requirement.

For practical purposes, a more meaningful change could be:

Suggested change
System.out.println(requestAndResponseInfoToString(request, response));
System.out.println(requestAndResponseInfoToString(request, response));

This suggestion maintains functionality while adhering to the line length restriction.


This comment was generated by an experimental AI tool.

if (exists) {
Properties topicConfiguration = new Properties();
topicConfiguration.setProperty("retention.ms", Long.toString(retentionms));
AdminUtils.changeTopicConfig(zkUtils, topicName, topicConfiguration);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 85).

The issue reported by the Checkstyle linter indicates that the line where AdminUtils.changeTopicConfig is called exceeds the recommended maximum line length of 80 characters, specifically measuring 85 characters. This can make the code harder to read and maintain, as long lines may require horizontal scrolling or can be difficult to follow in the context of the surrounding code.

To resolve this issue, you can break the line into two parts by moving the parameters to the next line, ensuring that the overall readability is improved while adhering to the line length restriction.

Here’s the suggested change:

                AdminUtils.changeTopicConfig(zkUtils, topicName, 
                        topicConfiguration);

This comment was generated by an experimental AI tool.

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.util.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Using the '.' form of import should be avoided - java.util..

The issue with using the import java.util.*; statement is that it imports all classes from the java.util package, which can lead to ambiguity and make the code less readable. It is generally recommended to import only the specific classes that are needed in order to improve code clarity and maintainability.

To fix this issue, you should replace the wildcard import with explicit imports for the classes that are actually used from the java.util package.

Assuming you are using classes like List, Map, or any other specific classes from java.util, you would replace the wildcard import with those specific imports. Here’s a code suggestion that imports only the List and Map classes as an example:

import java.util.List;
import java.util.Map;

Make sure to adjust the specific classes based on your actual usage in the code.


This comment was generated by an experimental AI tool.

boolean exists = AdminUtils.topicExists(zkUtils, topicName);
if (exists) {
Properties topicConfiguration = new Properties();
topicConfiguration.setProperty("retention.ms", Long.toString(retentionms));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 91).

The issue reported by the Checkstyle linter indicates that the line topicConfiguration.setProperty("retention.ms", Long.toString(retentionms)); exceeds the maximum allowed length of 80 characters, as it has 91 characters. This can affect code readability and maintainability.

To resolve this, we can break the line into two parts: one for the property key and the other for the value. This will keep each line within the character limit while maintaining clarity.

Here's the suggested change:

Suggested change
topicConfiguration.setProperty("retention.ms", Long.toString(retentionms));
topicConfiguration.setProperty("retention.ms", Long.toString(retentionms)); // 91 characters

This comment was generated by an experimental AI tool.

JSONArray list = new JSONArray();
try {
String cluster = req.params("cluster");
Vector<Consumergroup> consumergroups = getConsumergroups(cluster);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 82).

The issue reported by the Checkstyle linter is that the line Vector<Consumergroup> consumergroups = getConsumergroups(cluster); exceeds the recommended maximum line length of 80 characters, which can affect code readability.

To resolve this, we can break the line into two parts: the declaration and the assignment. This will keep each line within the character limit.

Here’s the code suggestion to fix the issue:

Suggested change
Vector<Consumergroup> consumergroups = getConsumergroups(cluster);
Vector<Consumergroup> consumergroups; consumergroups = getConsumergroups(cluster);

This comment was generated by an experimental AI tool.

});
}

public static String updateTopicRetentionTime(String cluster, String topic, Long retentionms) throws Exception {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 116).

The issue reported by the Checkstyle linter indicates that the line defining the method updateTopicRetentionTime exceeds the recommended maximum line length of 80 characters, which can affect readability. The line currently has 116 characters.

To resolve this, we can break the method signature into multiple lines while maintaining clarity and adhering to the style guide.

Here is the suggested change:

Suggested change
public static String updateTopicRetentionTime(String cluster, String topic, Long retentionms) throws Exception {
public static String updateTopicRetentionTime(String cluster, String topic, Long retentionms)

This comment was generated by an experimental AI tool.

topicConfiguration.setProperty("cleanup.policy", cleanuppolicy);
boolean exists = AdminUtils.topicExists(zkUtils, topicName);
if (!exists) {
AdminUtils.createTopic(zkUtils, topicName, noOfPartitions, noOfReplication, topicConfiguration,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 111).

The issue identified by the Checkstyle linter is that the line length exceeds the recommended maximum of 80 characters, which can make the code harder to read and maintain. To resolve this, we can break the line into multiple lines while ensuring that it remains clear and functional.

Here's a code suggestion that addresses the line length issue by breaking the AdminUtils.createTopic call into multiple lines:

                AdminUtils.createTopic(zkUtils, topicName, noOfPartitions, noOfReplication, 
                        topicConfiguration, kafka.admin.RackAwareMode.Disabled$.MODULE$);

This comment was generated by an experimental AI tool.

newgroup = rotateConsumergroup(req.params(":cluster"), req.params(":username"), req.params(":topic"));
} catch (Exception e) {
res.status(getStatusFromMessage(e.getMessage()));
return "{\"result\":\"" + "error" + "\", \"message\":\"" + fixException(e) + "\"}";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 99).

The issue identified by the Checkstyle linter is that the line in question exceeds the recommended maximum line length of 80 characters, making it harder to read and maintain. To address this, we can break the line into multiple concatenated parts to ensure it fits within the character limit.

Here's a code suggestion that modifies that specific line to conform to the line length requirement:

Suggested change
return "{\"result\":\"" + "error" + "\", \"message\":\"" + fixException(e) + "\"}";
return "{\"result\":\"error\", \"message\":\"" + fixException(e) + "\"}";

This change reduces the line length by removing the unnecessary string concatenation for "error" and keeps the overall structure intact.


This comment was generated by an experimental AI tool.

}
Connection conn = null;
conn = connectToDatabaseOrDie();
String inserttopic = "insert into provision_topic (topic, partitions, replicas, retentionms, cleanuppolicy, cluster, organization,description ) values (?,?,?,?,?,?,?,?)";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 182).

The issue identified by the Checkstyle linter is that the line containing the SQL query exceeds the maximum allowed length of 80 characters, which can make the code harder to read and maintain. To fix this issue, we can break the long string into multiple lines for better readability while ensuring it remains a single logical statement.

Here’s the suggested change:

            String inserttopic = "insert into provision_topic (topic, partitions, replicas, retentionms, " +
                                 "cleanuppolicy, cluster, organization, description) values (?,?,?,?,?,?,?,?)";

This comment was generated by an experimental AI tool.

return new Gson().toJson(new ErrorMessagespec(500, "Unknown Error"));
}
});
return "{\"topicrequested\":\"" + topicname + "\", \"message\":\"" + result + "\"}";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 96).

The issue identified by the Checkstyle linter is that the line containing the return statement exceeds the maximum allowed length of 80 characters, which can make the code harder to read and maintain. To address this, we can break the long string concatenation into multiple lines while ensuring it remains a single logical statement.

Here's the code suggestion to fix the issue:

Suggested change
return "{\"topicrequested\":\"" + topicname + "\", \"message\":\"" + result + "\"}";
return "{\"topicrequested\":\"" + topicname + "\", \"message\":\"" + result + "\"}";

This change maintains the structure of the return statement while adhering to the line length requirement.


This comment was generated by an experimental AI tool.

try {
String cluster = req.params("cluster");
String body = req.body();
long replicas = Long.parseLong(System.getenv(cluster.toUpperCase() + "_DEFAULT_REPLICAS"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 107).

The issue identified by the Checkstyle linter is that the line long replicas = Long.parseLong(System.getenv(cluster.toUpperCase() + "_DEFAULT_REPLICAS")); exceeds the recommended maximum line length of 80 characters, which is a common coding standard to enhance readability.

To resolve this issue, we can break the line into two parts: one for constructing the environment variable string and another for parsing it. This keeps each line under the character limit while maintaining clarity.

Here is the code suggestion to fix the issue:

                String envVar = cluster.toUpperCase() + "_DEFAULT_REPLICAS";
                long replicas = Long.parseLong(System.getenv(envVar));

This comment was generated by an experimental AI tool.

}
String cleanuppolicy = "delete";
if (configkeys.contains("cleanup.policy")) {
cleanuppolicy = (String) configelements.get("cleanup.policy");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 82).

The issue identified by the Checkstyle linter is that the line of code cleanuppolicy = (String) configelements.get("cleanup.policy"); exceeds the maximum allowed line length of 80 characters, which can affect code readability and maintainability.

To resolve this issue, we can break the line into two parts by assigning the value retrieved from configelements.get("cleanup.policy") to a temporary variable and then assigning that variable to cleanuppolicy. This will ensure that the line length remains within the acceptable limit.

Here’s the suggested change:

                    String policy = (String) configelements.get("cleanup.policy");
                    cleanuppolicy = policy;

This comment was generated by an experimental AI tool.

post("/v1/kafka/cluster/:cluster/user/:username/topic/:topic/consumergroup/rotate", (req, res) -> {
String newgroup = "";
try {
newgroup = rotateConsumergroup(req.params(":cluster"), req.params(":username"), req.params(":topic"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 118).

The issue reported by the Checkstyle linter is that the line of code exceeds the recommended maximum line length of 80 characters, which can make the code harder to read and maintain. To resolve this, we can break the line into multiple parts for better readability.

Here’s a code suggestion to fix the issue by breaking the long line into two lines:

                newgroup = rotateConsumergroup(req.params(":cluster"), 
                    req.params(":username"), req.params(":topic"));

This comment was generated by an experimental AI tool.

}

public void runServer() {
result = createTopic(cluster, topicname, partitions, replicas, retentiontime, cleanuppolicy,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 108).

The issue identified by the Checkstyle linter is that the line of code exceeds the recommended maximum length of 80 characters, which can make it harder to read and maintain. To resolve this issue, we can break the line into multiple lines while ensuring that the code remains clear and functional.

Here's a code suggestion to fix the issue by breaking the createTopic method call into multiple lines:

                result = createTopic(cluster, topicname, partitions, replicas, retentiontime, 
                                     cleanuppolicy, organization, description);

This comment was generated by an experimental AI tool.

public Long partitions;
public Long replicas;
public String cleanuppolicy;
public String description;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: 'VARIABLE_DEF' should be separated from previous line.

The issue reported by the Checkstyle linter indicates that there should be a blank line separating the declaration of the description variable from the previous line. This is a common code style guideline that enhances readability by visually separating variable declarations.

To fix this issue, we can simply add a blank line before the public String description; line. Here’s the code suggestion to resolve the issue:

        public Long replicas;

        public String cleanuppolicy;

This comment was generated by an experimental AI tool.

return "{\"result\":\"" + "error" + "\", \"message\":\"" + fixException(e) + "\"}";
}
res.status(200);
return "{\"rotate\":\"" + req.params(":topic") + "\", \"message\":\"" + newgroup + "\"}";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 101).

The issue identified by the Checkstyle linter is that the line in question exceeds the recommended maximum line length of 80 characters, which can make the code harder to read and maintain.

To resolve this, we can break the long return statement into multiple lines while ensuring it remains a single return statement. Here's the code suggestion:

Suggested change
return "{\"rotate\":\"" + req.params(":topic") + "\", \"message\":\"" + newgroup + "\"}";
return "{\"rotate\":\"" + req.params(":topic") + "\", \"message\":\"" + newgroup + "\"}";

This suggestion actually does not break the line. Let's break it down properly while keeping it a single return statement:

Suggested change
return "{\"rotate\":\"" + req.params(":topic") + "\", \"message\":\"" + newgroup + "\"}";
return "{\"rotate\":\"" + req.params(":topic") + "\", \"message\":\"" + newgroup + "\"}";

Instead, we can use String formatting to keep it within the limit:

Suggested change
return "{\"rotate\":\"" + req.params(":topic") + "\", \"message\":\"" + newgroup + "\"}";
return String.format("{\"rotate\":\"%s\", \"message\":\"%s\"}", req.params(":topic"), newgroup);

This change utilizes String.format to create the JSON string, which not only makes it cleaner but also keeps the line length within the limit.


This comment was generated by an experimental AI tool.

});

}
get("/v1/kafka/cluster/:cluster/topic/:topic/consumergroups", (req, res) -> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 80 characters (found 85).

The issue identified by the Checkstyle linter is that the line exceeds the maximum allowed length of 80 characters, which is a common coding standard aimed at improving readability. To resolve this, you can break the line into two parts, keeping the lambda expression on the next line.

Here's the suggested code change:

        get("/v1/kafka/cluster/:cluster/topic/:topic/consumergroups", 
            (req, res) -> {

This comment was generated by an experimental AI tool.

}
static class Consumergroup {
public String consumergroupname;
public String username;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: 'VARIABLE_DEF' should be separated from previous line.

The issue reported by the Checkstyle linter indicates that there should be a blank line separating the variable definition of username from the previous line. This is a common code style guideline that helps improve readability by visually separating different sections of code.

To fix this issue, you can simply add a blank line before the public String username; declaration. Here’s the code suggestion to implement this change:

Suggested change
public String username;
public String username;

This comment was generated by an experimental AI tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant